本篇會簡單講述使用 StyleCop.Analyzers 為專案取得測試覆蓋率。
首先,先在測試專案中,新增 StyleCop.Analyzers
套件:
# Location: ~/dotnet-sln
$ cd WebProj
# Location: ~/dotnet-sln/WebProj
$ dotnet add package StyleCop.Analyzers
info : Removing PackageReference for package 'StyleCop.Analyzers' from project '/Users/ironman/dotnet-sln/WebProj/WebProj.csproj'.
error: Project '/Users/ironman/dotnet-sln/WebProj/WebProj.csproj' does not contain any PackageReference 'StyleCop.Analyzers' to Remove.
接著去執行 dotnet build
。
$ dotnet test -p:CollectCoverage=true
Writing /var/folders/hx/lgdjx1l93pq08s35hhwb0py40000gn/T/tmpoRUY4i.tmp
info : Adding PackageReference for package 'StyleCop.Analyzers' into project '/Users/ironman/dotnet-sln/WebProj/WebProj.csproj'.
info : Restoring packages for /Users/ironman/dotnet-sln/WebProj/WebProj.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/stylecop.analyzers/index.json
info : OK https://api.nuget.org/v3-flatcontainer/stylecop.analyzers/index.json 2211ms
info : GET https://api.nuget.org/v3-flatcontainer/stylecop.analyzers/1.1.118/stylecop.analyzers.1.1.118.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/stylecop.analyzers/1.1.118/stylecop.analyzers.1.1.118.nupkg 53ms
info : Installing StyleCop.Analyzers 1.1.118.
info : Package 'StyleCop.Analyzers' is compatible with all the specified frameworks in project '/Users/ironman/dotnet-sln/WebProj/WebProj.csproj'.
info : PackageReference for package 'StyleCop.Analyzers' version '1.1.118' added to file '/Users/ironman/dotnet-sln/WebProj/WebProj.csproj'.
info : Committing restore...
info : Generating MSBuild file /Users/ironman/dotnet-sln/WebProj/obj/WebProj.csproj.nuget.g.props.
info : Generating MSBuild file /Users/ironman/dotnet-sln/WebProj/obj/WebProj.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/ironman/dotnet-sln/WebProj/obj/project.assets.json
log : Restore completed in 3.69 sec for /Users/ironman/dotnet-sln/WebProj/WebProj.csproj.
fntsr@fntsrde-MacBook-Pro ~/P/I/d/WebProj> dotnet build
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 400.75 ms for /Users/ironman/dotnet-sln/WebProj/WebProj.csproj.
Controllers/HomeController.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(8,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(9,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(8,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(9,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(10,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(11,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(48,17): warning SA1515: Single-line comment should be preceded by blank line [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(33,1): warning SA1507: Code should not contain multiple blank lines in a row [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(19,13): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(20,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(15,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(9,60): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(26,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(26,82): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
WebProj -> /Users/ironman/dotnet-sln/WebProj/bin/Debug/netcoreapp2.2/WebProj.dll
WebProj -> /Users/ironman/dotnet-sln/WebProj/bin/Debug/netcoreapp2.2/WebProj.Views.dll
Build succeeded.
Controllers/HomeController.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(8,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(9,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(2,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(3,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(4,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(5,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(6,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(7,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(8,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(9,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(10,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(11,1): warning SA1200: Using directive should appear within a namespace declaration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(48,17): warning SA1515: Single-line comment should be preceded by blank line [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(33,1): warning SA1507: Code should not contain multiple blank lines in a row [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Startup.cs(19,13): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(20,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(15,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Models/ErrorViewModel.cs(9,60): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(26,20): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
Controllers/HomeController.cs(26,82): warning SA1101: Prefix local calls with this [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [/Users/ironman/dotnet-sln/WebProj/WebProj.csproj]
41 Warning(s)
0 Error(s)
Time Elapsed 00:00:05.20
去修改訊息回饋的 warning,讓他歸零。
$ dotnet build
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 36.26 ms for /Users/ironman/dotnet-sln/WebProj/WebProj.csproj.
WebProj -> /Users/ironman/dotnet-sln/WebProj/bin/Debug/netcoreapp2.2/WebProj.dll
WebProj -> /Users/ironman/dotnet-sln/WebProj/bin/Debug/netcoreapp2.2/WebProj.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.26